package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.VipHistoryListBO;
import com.junzhiit.nengji.shop.dao.VipHistoryDAO;
import com.junzhiit.nengji.shop.model.VipHistory;

@Repository
public class VipHistoryDAOImpl extends AbstractBaseDAO<VipHistory>implements VipHistoryDAO {

	@Override
	public List<VipHistoryListBO> query(VipHistoryListBO vhlb, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select");
		sql.append(" u.name,u.mobile_phone,u.country,u.province,u.city,u.is_shop,v.id vip_id,v.identity,");
		sql.append(" v.target,vh.pay_money,vh.pay_time,vh.pay_way,vh.status,vh.purchase_type,vh.created_time");
		sql.append(" from vip_history vh");
		sql.append(" inner join user u on vh.user_id=u.id");
		sql.append(" inner join vip v on vh.vip_id=v.id");
		sql.append(" where vh.is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		if(vhlb.getStartTime()!=null&&vhlb.getEndTime()!=null){
			sql.append(" and vh.created_time between ? and ?");
			param.setDate(vhlb.getStartTime());
			param.setDate(vhlb.getEndTime());
		}
		if (StringUtils.isNotBlank(vhlb.getName())) {
			sql.append(" and u.name like ?");
			param.setString("%" + vhlb.getName()+ "%");
		}
		if(vhlb.getStatus()!=null){
			sql.append(" and vh.status=?");
			param.setInt(vhlb.getStatus());
		}
		if (vhlb.getIdentity() != null) {
			sql.append(" and v.identity=?");
			param.setInt(vhlb.getIdentity());
		}
		sql.append(" order by vh.created_time desc");
		return super.queryForPagingList(sql.toString(), VipHistoryListBO.class, param,pageNumber,maxPageRow);
	}

	@Override
	public Integer getTotalCount(VipHistoryListBO vhlb) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select");
		sql.append(" count(vh.id)");
		sql.append(" from vip_history vh");
		sql.append(" inner join user u on vh.user_id=u.id");
		sql.append(" inner join vip v on vh.vip_id=v.id");
		sql.append(" where vh.is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		if(vhlb.getStartTime()!=null&&vhlb.getEndTime()!=null){
			sql.append(" and vh.created_time between ? and ?");
			param.setDate(vhlb.getStartTime());
			param.setDate(vhlb.getEndTime());
		}
		if (StringUtils.isNotBlank(vhlb.getName())) {
			sql.append(" and u.name like ?");
			param.setString("%" + vhlb.getName()+ "%");
		}
		if(vhlb.getStatus()!=null){
			sql.append(" and vh.status=?");
			param.setInt(vhlb.getStatus());
		}
		if (vhlb.getIdentity() != null) {
			sql.append(" and v.identity=?");
			param.setInt(vhlb.getIdentity());
		}
		sql.append(" order by vh.created_time desc");
		return super.queryForInt(sql.toString(), param);
	}
}
